package com.sky.controller.user;

import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "移动端菜品")
@RequestMapping("user/dish")
@Slf4j
public class UserDishController {
    @Autowired
    private DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    @ApiOperation("根据分类查询菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> list(Long categoryId){
        String key = "dish_"+categoryId;
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<DishVO> redisList = (List<DishVO>) valueOperations.get(key);
        if(redisList == null){
            List<DishVO> list = dishService.listWithFlavor(categoryId);
            valueOperations.set(key,list);
            return Result.success(list);
        }
        return Result.success(redisList);
    }
}
